/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service.share;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.boxin.ims.modules.wechat.entity.share.ShareRule;
import com.boxin.ims.modules.wechat.dao.share.ShareRuleDao;

/**
 * 分享转发中奖规则Service
 * @author Jakemanse
 * @version 2013-10-01
 */
@Component
@Transactional(readOnly = true)
public class ShareRuleService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(ShareRuleService.class);
	
	@Autowired
	private ShareRuleDao shareRuleDao;
	
	public ShareRule get(Long id) {
		return shareRuleDao.findOne(id);
	}
	
	public List<ShareRule> findByUserId(Long userId){
		
		DetachedCriteria dc = shareRuleDao.createDetachedCriteria();
		dc.add(Restrictions.eq("user.id", userId));
		dc.add(Restrictions.eq("delFlag", ShareRule.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return shareRuleDao.find(dc);
	}
	
	public Page<ShareRule> find(Page<ShareRule> page, ShareRule shareRule) {
		DetachedCriteria dc = shareRuleDao.createDetachedCriteria();
		if (shareRule.getUser()!=null && shareRule.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", shareRule.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(shareRule.getName())){
			dc.add(Restrictions.like("name", "%"+shareRule.getName()+"%"));
		}
		if (StringUtils.isNotEmpty(shareRule.getRemarks())){
			dc.add(Restrictions.like("remarks", "%"+shareRule.getRemarks()+"%"));
		}
		dc.add(Restrictions.eq("delFlag", ShareRule.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return shareRuleDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(ShareRule shareRule) {
		shareRuleDao.save(shareRule);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		shareRuleDao.deleteById(id);
	}
	
}
